# -*- coding:utf-8 -*-
# @Author:🎈RedBalloon
# @Time:2022/9/28-23:51
# @File:06-post请求的使用.py


from urllib.request import Request, urlopen
from urllib.parse import urlencode
from fake_useragent import UserAgent

# 在Request请求对象中有data参数，它就是用在POST请求中的，要传递的POST请求参数data是一个字典
# 模拟登录笔趣阁
url = "https://www.bbiquge.net/login.php?do=submit"
headers = {
    "User-Agent": UserAgent().random,
    "referer": "https://www.bbiquge.net/login.php"
}

# form data就是POST要传入的参数
from_data = {
    "username": "redballoon",
    "password": "qwer123456",
    # 传递的验证码
    "checkcode": "6591",
    "usecookie": "315360000",
    "action": "login",
    "submit": "%C1%A2%BC%B4%B5%C7%C2%BC"
}

# 在传递POST参数中出现中文的情况都需要转码，
data = urlencode(from_data).encode()
print(data)
request = Request(url, data=data, headers=headers)
response = urlopen(request).read()
print(response)

# 笔趣阁加了验证码（难搞，后面学习到再回来）
